/*
 * @Des: 流程控制bpmApi
 * @Author: jin_yc
 * @Date: 2019-11-13 16:07:37
 * @LastEditor: jin_yc
 * @LastEditTime: 2020-07-15 09:12:08
 */

import request from '@/utils/request'
import store from '@/store'

/**
 * @description: 废弃流程
 * @path { taskId } 任务id 必填
 * @param { comment } 废弃原因
 */
export function processAbandon(data) {
  return request({
    method: 'delete',
    url: '/sys-bpm/process/',
    params: {
      taskId: data.taskId,
      comment: data.comment || ''
    }
  })
}
/**
 * @description: 启动流程
 * @param { modelKey } 流程模型Key 必填
 * @param { stageFlag } 暂存标识 0（不暂存）/ 1（暂存）默认不暂存
 * @param { bizId } 表单业务数据ID
 * @param { formKey } 表单Key
 * @param { customProcessFlag } 自由流程表单 标识符（与普通表单区分）
 * @body { variable } 流程变量 / 审批人 JSON放在body中
 */
export function processStart(data) {
  return request({
    method: 'post',
    url: '/sys-bpm/process/start',
    params: {
      modelKey: data.modelKey || '',
      comment: data.comment,
      bizId: data.bizId || '',
      formKey: data.formKey || '',
      customProcessFlag: data.customProcessFlag || '',
      stageFlag: data.stageFlag || '',
      sourceId: store.state.portal.id || '', //门户ID
      sourceName: store.state.portal.name || '' //门户name
    },
    data: data.variable || '',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}

/**
 * @description: 审批通过流程
 * @param { taskId } 任务id必传
 * @param {approval} 审批类型
 * @param {comment} 审批意见
 * @param {stageFlag} 暂存标识 0（不暂存）/1（暂存）
 * @body { variable } 流程变量 / 审批人
 */
export function processComplete(data) {
  return request({
    method: 'put',
    url: '/sys-bpm/process/complete',
    params: {
      taskId: data.taskId,
      bizId: data.bizId || '',
      approval: data.approval || '',
      comment: data.comment || '',
      stageFlag: data.stageFlag || '0'
    },
    data: data.variable,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}

/**
 * @description: 加签减签
 * @param { type } 加签/减签 add/delete 必传
 * @param { taskId } 任务Id 必传
 * @param {taskKey} 指定节点，不传默认当前节点
 * @body {userNameList} 加签/减签用户列表
 */
export function processMulti(type, taskId, taskKey, userNameList) {
  return request({
    method: 'put',
    url: '/sys-bpm/process/multi/' + type + '/',
    params: {
      taskId: taskId,
      taskKey: taskKey || '',
      userNames: userNameList || ''
    },
    transformRequest: [
      (data) => {
        return data
      }
    ],
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}
/**
 * @description: 流程驳回
 * @param {taskId} 任务Id
 * @param {comment} 审批意见
 * @param { targetKey } 驳回节点Key， 不传默认驳回上一节点
 */
export function processReject(params) {
  return request({
    method: 'put',
    url: '/sys-bpm/process/reject',
    params: params
  })
}

/**
 * @description: 流程委托
 * @param {data}
 */
export function processDelegate(data) {
  return request({
    method: 'get',
    url: '/sys-bpm/process/delegate',
    params: {
      taskId: data.taskId,
      comment: data.comment || '',
      userName: data.userName || ''
    }
  })
}

/**
 * @description: 查询当前节点按钮信息
 * @param { taskId } 任务id
 * @return:
 */
export function processButton(taskId) {
  return request({
    method: 'get',
    url: '/sys-bpm/process/button',
    params: {
      taskId: taskId
    }
  })
}
/**
 * @description: 查询当前流程所有节点的key
 * @param { processDefinitionId } 流程图ID
 * @param {taskId} 任务id
 */
export function processTaskKey(taskId) {
  return request({
    method: 'get',
    url: '/sys-bpm/process/taskKey',
    params: {
      taskId: taskId
    }
  })
}

// 获取自由流程流程节点审批数据
export function getCustomNodeDetail(processInstanceId) {
  return request({
    url: '/sys-bpm/custom/node/detail',
    method: 'get',
    params: {
      processInstanceId: processInstanceId
    }
  })
}

// 获取流程节点审批数据（自由流程），需要包含processInstanceId
export function saveCustomNodeDetail(data) {
  return request({
    url: '/sys-bpm/custom/node/detail',
    method: 'post',
    data: data
  })
}
export function circulation(data) {
  return request({
    url: '/sys-bpm/circulation',
    method: 'post',
    data: data
  })
}
